package cate.game.activity.destinygate;

import cate.common.table.activity.destinygate.row.DestinyGateChapterRow;
import cate.common.table.d.GDFight;
import cate.common.table.d.GDOperation;
import cate.common.table.farm.fbchallenge.row.FbChallengeRow;
import cate.game.activity.destinygate.msg.DestinyGateChallengeResultResp;
import cate.game.activity.destinygate.po.DestinyGateChapter;
import cate.game.activity.destinygate.po.DestinyGateLevel;
import cate.game.play.proce.play.PlayPO;
import cate.game.res.MixRes;
import cate.game.role.Role;
import cate.game.role.farm.AbstractFarmFTC;
import cate.game.role.farm.FightChallengeHandler;

import java.util.ArrayList;
import java.util.List;

public class DestinyGateFTC extends AbstractFarmFTC<DestinyGateActivity> {
    /**
     * 当前挑战关卡的模板
     */
    private final DestinyGateChapterRow row;
    @Override
    protected DestinyGateActivity po(Role role) {
        return role.getActivity().getEnableActivitySingle(funcId);
    }

    public DestinyGateFTC(Role role, DestinyGateChapterRow row, int funcId) {
        this.funcId = funcId;
        this.row = row;
        setup(role);
    }

    @Override
    public void handleResult(Role role, PlayPO playPO) {
        super.handleResult(role, playPO);
        boolean win = playPO.winner == GDFight.Team.A;
        DestinyGateActivity destinyGate = po(role);
        //处理战斗返回
        DestinyGateChallengeResultResp resp = new DestinyGateChallengeResultResp();
        resp.win = win;
        //获胜
        if (win) {
            DestinyGateChapter chapter = destinyGate.showChapter(row.heroId);
            DestinyGateLevel level = chapter.showLevel(row.id);
            MixRes reward = new MixRes();
            MixRes achieveReward = new MixRes();
            //首通奖励
            if (!level.pass) {
                role.log.setOperation(GDOperation.DESTINY_GATE);
                reward.addList(new MixRes(row.firstRewardStr));
                reward.add(role, true, GDOperation.DESTINY_GATE);
                role.log.clearOperation();
            }
            resp.reward = reward;
            level.pass = true;
            // 本次战斗有没有完成新的挑战
            boolean challengeChange = false;
            if (level.challenge.size() < row.challengeIds.size() || level.achieve.size() < row.achieveIds.size()) {
                // 这里要检测战斗挑战的完成情况
                List<Integer> starDone = new ArrayList<>();
                List<Integer> achieveDone = new ArrayList<>();
                //星级挑战
                for (int challengeId : row.challengeIds) {
                    FbChallengeRow challengeRow = role.getGame().table.fbChallenge.spiceFb.get(challengeId);
                    if (challengeRow == null) {
                        continue;
                    }
                    boolean done = FightChallengeHandler.checkDone(role.getGame(), playPO, challengeRow);
                    if (done) {
                        starDone.add(challengeId);
                    }
                }
                if (starDone.size() >= level.challenge.size()) {
                    // 只检测之前没有完成的星级挑战
                    for (int challengeId : starDone) {
                        if (!level.challenge.contains(challengeId)) {
                            challengeChange = true;
                            level.challenge.add(challengeId);
                        }
                    }
                }
                //成就挑战
                for (int challengeId : row.achieveIds) {
                    FbChallengeRow challengeRow = role.getGame().table.fbChallenge.spiceFb.get(challengeId);
                    if (challengeRow == null) {
                        continue;
                    }
                    boolean done = FightChallengeHandler.checkDone(role.getGame(), playPO, challengeRow);
                    if (done) {
                        achieveDone.add(challengeId);
                    }
                }
                if (achieveDone.size() > 0 && achieveDone.size() >= level.achieve.size()) {
                    // 只检测之前没有完成的成就挑战
                    for (int challengeId : achieveDone) {
                        if (!level.achieve.contains(challengeId)) {
                            FbChallengeRow challengeRow = role.getGame().table.fbChallenge.spiceFb.get(challengeId);
                            challengeChange = true;
                            level.achieve.add(challengeId);
                            achieveReward.addList(new MixRes(challengeRow.rewardStr));
                        }
                    }
                }
            }
            achieveReward.add(role, true, GDOperation.DESTINY_GATE);
            resp.achievementReward = achieveReward;
            //有挑战更新
            if (challengeChange) {
                chapter.updateChapter();
            }
            destinyGate.noticeUpdate();
        }
        role.sendNow(resp);

    }
}
